package com.rocching.easychat.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.rocching.easychat.R;
import com.rocching.easychat.core.UserIdentityContext;
import com.rocching.easychat.model.FriendModel;
import com.rocching.easychat.util.AppUtil;
import com.rocching.easychat.util.StringUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class FriendListAdapter extends BaseAdapter {
    private final Context context;
    private List<FriendModel> list;
    private Map<Long, Boolean> map;

    public FriendListAdapter(Context context) {
        this.context = context;
        list = new ArrayList<>();
        map = new HashMap<>();
    }

    public void update(List<FriendModel> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        FriendModel model = list.get(i);
        if (Objects.isNull(model)) {
            return 0;
        }
        return model.getId();
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        FriendListHolder holder = null;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.friend_list_item, null);
            holder = new FriendListHolder(view);
            view.setTag(holder);
        } else {
            holder = (FriendListHolder) view.getTag();
        }
        FriendModel model = list.get(i);
        if (!Objects.isNull(model)) {
            holder.update(model);
        }
        return view;
    }

    public int selected(int i, View view) {
        FriendModel model = list.get(i);
        if (Objects.isNull(model)) {
            return 0;
        }

        Boolean flag = false;
        if (map.containsKey(model.getId())) {
            flag = map.get(model.getId());
        }
        flag = !flag;
        map.put(model.getId(), flag);

        int count = 0;
        for (Long key : map.keySet()) {
            Boolean f = map.get(key);
            if (f != null && f) {
                count++;
            }
        }

        FriendListHolder holder = (FriendListHolder) view.getTag();
        holder.updateSelected(flag);

        return count;
    }

    public List<Long> getAllSelected() {
        List<Long> all = new ArrayList<>();
        for (Long key : map.keySet()) {
            Boolean flag = map.get(key);
            if (flag != null && flag) {
                all.add(key);
            }
        }
        return all;
    }

    public String getSelectedUserName() {
        List<Long> all = getAllSelected();
        if (StringUtil.isEmpty(all)) {
            return "";
        }
        List<String> names = new ArrayList<>();
        names.add(UserIdentityContext.getUserName());
        for (Long id : all) {
            FriendModel model = list.stream().filter(m -> id.equals(m.getId())).findFirst().orElse(null);
            if (model != null) {
                names.add(model.getName());
            }
        }

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < names.size(); i++) {
            sb.append(names.get(i));
            if (i < names.size() - 1) {
                sb.append(",");
            }
        }
        return sb.toString();
    }

    public static class FriendListHolder {
        private final View view;
        private final ImageView friendPhoto;
        private final ImageView selectedPhoto;
        private final TextView friendName;

        public FriendListHolder(View view) {
            this.view = view;

            friendPhoto = view.findViewById(R.id.friend_photo);
            selectedPhoto = view.findViewById(R.id.selected_photo);
            friendName = view.findViewById(R.id.friend_name);
        }

        public void update(FriendModel model) {
            updateSelected(false);
            friendName.setText(model.getFriendName());
            Uri uri = AppUtil.getUserPhotoUri(model.getPhoto());
            if (uri != null) {
                Glide.with(view).load(uri).placeholder(R.drawable.loading).into(friendPhoto);
            }
        }

        public void updateSelected(boolean flag) {
            Context context = view.getContext();
            if (context == null) {
                return;
            }
            int resId = flag ? R.drawable.selected : R.drawable.no_selected;
            selectedPhoto.setImageDrawable(context.getDrawable(resId));
        }
    }
}
